package ar.com.spsolutions.splilabrary.view.pages.components;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.ResourceModel;

public class PopinNotifier extends Panel implements IHeaderContributor {

	private static final long serialVersionUID = 6491070976523772879L;
	private String title;
	private final Label text;
	private Label icon;

	public PopinNotifier(final String id) {
		super(id);
		this.text = new Label("notifierText", new ResourceModel("notifierText",
				""));
		this.icon = new Label("icon", "");
		this.add(this.icon);
		this.add(this.text);
		this.setOutputMarkupId(true);
		this.setOutputMarkupPlaceholderTag(true);
		this.setVisible(false);
	}

	@Override
	protected void onComponentTag(final ComponentTag tag) {
		super.onComponentTag(tag);
		tag.setId(this.getId());
		this.renderTitle(tag);
		this.renderIcon();
	}

	private void renderIcon() {
		this.getIcon().add(
				new AttributeAppender("class", "ui-icon "
						+ this.getIcon().getDefaultModelObjectAsString()));
	}

	private void renderTitle(final ComponentTag tag) {
		if (this.getTitle() == null) {
			tag.getAttributes().remove("title");
		} else {
			tag.getAttributes().put("title", this.getTitle());
		}
	}

	@Override
	public void renderHead(final IHeaderResponse response) {
		super.renderHead(response);
		response.renderJavaScriptReference("scripts/jquery-1.6.2.js");
		response.renderJavaScriptReference("scripts/jquery-ui-1.8.16.min.js");
		response.renderCSSReference("theme/jquery-ui-1.8.16.css");
	}

	public void info(final String notifyTitle, final String message,
			final AjaxRequestTarget target) {
		this.getIcon().setDefaultModelObject("ui-icon-circle-check");
		this.renderMessage(notifyTitle, message, target);
	}

	public void error(final String notifyTitle, final String message,
			final AjaxRequestTarget target) {
		this.getIcon().setDefaultModelObject(".ui-icon-circle-close");
		this.renderMessage(notifyTitle, message, target);
	}

	private void renderMessage(final String notifyTitle, final String message,
			final AjaxRequestTarget target) {
		this.setTitle(notifyTitle);
		this.getText().setDefaultModel(new ResourceModel(message, message));
		this.setVisible(true);
		target.add(this);
		target.appendJavaScript("$(function() {"
				+ //
				"$('#" + this.getId() + ":ui-dialog').dialog('destroy');"
				+ "$('#" + this.getId() + "').dialog({" + //
				"modal: true," + //
				"buttons: {" + //
				"Ok: function() {" + //
				"$( this ).dialog('close');" + "}" + //
				"}" + //
				"})" + //
				"})");
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(final String title) {
		this.title = title;
	}

	public Label getText() {
		return this.text;
	}

	public Label getIcon() {
		return this.icon;
	}

	public void setIcon(final Label icon) {
		this.icon = icon;
	}

}
